<?php
require_once('entities/User.php');

class UserModel
{
	public $user;
	public function __contruct()
	{
		
	}
	public function adduser($username,$password,$name,$gender,$dob ,$address,$email,$phone)
	{
		global $entityManager;
		$this->user = new User();
		$this->user->setUserName($username);
		$this->user->setPassWord($password);
		$this->user->setName($name);
		$this->user->setGender($gender);
		$this->user->setDob(new \DateTime($dob));
		$this->user->setAddress($address);
		$this->user->setEmail($email);
		$this->user->setPhone($phone);
		$this->user->setGroupId("2");

		$entityManager->persist($this->user);
		$entityManager->flush();
		header('Location: index.php?com=com_user&view=info');
		// $results = $entityManager->getRepository('User')->findAll();
		// foreach ($results as $a) {
		// 	echo $a->getName()." ----".$a->getAddress();
		// }
	}
	public function check_exists_user($username){
		global $entityManager;
		$results = $entityManager->getRepository('User')->findBy( array('username' => $username ));
		if ($results) {
			return true;
		}
		else return false;
	}
	public function login($username,$password)
	{
		global $entityManager;
		$results = $entityManager->getRepository('User')->findBy( array('username' => $username , 'password' => $password ));
		 if($results) 
		{
			foreach ($results as $result) {
				$_SESSION['username'] = $result->getUserName();
				header('Location: index.php?com=com_user&view=info');
			}
			
		}
	}
}